<?php

/**
 * @Author:      zwh
 * @DateTime:    2016-1-15 10:21:01
 * @Description: 用于订单企业套餐信息变更
 */
class ScsEnterpriseController extends BaseController
{
     /**
     * @Author:      zwh
     * @DateTime:    2016-6-28 16:25:17
     * @Description: 企业套餐列表
     */
    public function esList(){
    	$validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'user_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
    	do{
    		if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $es=EnterpriseSetmeal::where('status',1)->where('user_id',Input::get('user_id'))->select('id','name')->get();
            $temp_arr=array("count"=>sizeof($es),"es_package"=>$es);
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $temp_arr);
    	}while(false);
    	Log::info("info", array("system" => "scs" ,"event" => "service", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-6-28 13:58:17
     * @Description: 用于添加 订单企业包
     */
    public function addSetmeal(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'id'       => 'required',
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                $response = Lang::get("scs_error.order_status_error_40022");
                break;
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.car_model_is_empty23000");
                break;
            }
            $es = EnterpriseSetmeal::find(Input::get('id'));
            $pids = explode(',',$es->package_ids);
            $whp=WorkHoursPrice::where("city_id",$sc_user->shop->city_id)->first();
            if(is_null($whp)){
                $response = Lang::get("scs_error.work_hour_error_10001");
                break;
            }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }            
            $response=OrderProduct::implement($pids,$sc_user,$orders,$wmcf,$whp,$es,$csi);
        }while(false);
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-6-29 17:56:12
     * @Description: 删除产品包
     */
    public function delProduct(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                "order_product_id"  => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                if(!in_array($orders->status,Orders::$delete_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            } 
            $order_product=OrderProduct::find(Input::get("order_product_id"));
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $response=OrderProduct::deleteProducts($order_product,$sc_user->id);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getProductPackages", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-01-26 09:27:10
     * @Description: 用于更改企业套产品数量
     */
    public function changeEsItemNum(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_item_id'=> 'required',
                'number'=> 'required|numeric|min:0|not_in:0',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            $opesi=OrderProductEsItem::find(Input::get("order_product_item_id"));      
            if(is_null($opesi)||$opesi->order_id!=$orders->order_id||$opesi->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $opep=OrderProductEsPackage::find($opesi->order_product_package_id);
            $order_product=OrderProduct::find($opep->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $number=Input::get("number");
            $amount=Input::get("amount")*100;
            if($number==$opesi->num&&$amount==$opesi->amount){
            // if($number==$opesi->num){
                $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id,"order_product_amount"=>$order_product->amount,"order_amount"=>$orders->amount));
                break;
            }
            $response=OrderProductEsItem::changeProductItemNum($order_product,$opesi,$opep,$number,$amount,$sc_user->id);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeEsItemNum", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-01-28 09:24:52
     * @Description: 用于更换基础产品包的耗材型号
     */
    public function changeEsProductItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_esitem_id'=> 'required',//订单耗材的id
                'new_item_id'=> 'required',
                // 'type' => 'required'//0为标准耗材1为非标
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            $opei=OrderProductEsItem::find(Input::get("order_product_esitem_id"));
            if(is_null($opei)||$opei->order_id!=$orders->order_id||$opei->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $opep=OrderProductEsPackage::find($opei->order_product_package_id);
            $order_product=OrderProduct::find($opep->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.order_car_model_notexit_40002");
                break;
            }
            // $whp=WorkHoursPrice::where("city_id",$sc_user->shop->city_id)->first();
            // if(is_null($whp)){
            //     $response = Lang::get("scs_error.work_hour_error_10001");
            //     break;
            // }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }

            //要更换成的配件类型判断
            // if(Input::get('type')==0){
                $si=SupplyItem::find(Input::get("new_item_id"));
                $mw=MaintenanceWarehouse::where('item_id',$si->id)->where('shop_id',$sc_user->shop_id)->first();
                if(!$mw || $opei->num>$mw->number){
                    $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                    break;
                }
                
                //更换多城市价格
                $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$orders->city)->where("status",1)->first();
                if($sip){
                    $si->amount=$sip->price;
                }else{
                    $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                    $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                }

                if(Orders::isSale($si->id,$si->type)){
                    $response = Lang::get("scs_error.supply_error_10011");
                    break;
                }
                // $spi=SupplyProductItem::where("car_model_id",$csi->id)->where("supply_item_id",$si->id)->where("status",1)->first();
                // if(is_null($spi)){
                //     $response = Lang::get("scs_error.supply_item_car_model_error_41001");
                //     break;
                // }

            // }else{
            //     $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',1)->first();
            //     if(!$mw || $opei->num>$mw->number){
            //         $response = Lang::get("scs_error.supply_item_low_stocks_40005");
            //         break;
            //     }
            //     $si=SupplyItemNonStandard::find(Input::get("new_item_id"));
            //     if(Orders::isSale($si->id,1)){
            //         $response = Lang::get("scs_error.supply_error_10011");
            //         break;
            //     }
            //     $si->amount=Input::get("amount")*100;//如果是非标的 更新非标为传过来的价格
            // }

            //原有配件类型判断
            // if($opei->item_type==0){
            $old_si=SupplyItem::find($opei->item_id);
            // }else{
            //     $old_si=SupplyItemNonStandard::find($opei->item_id);
            // }
//             if(is_null($si)||$si->status!=1){
//                 $response = Lang::get("scs_error.supply_item_not_exit_41000");
//                 break;
//             }
            //如果更换非标不进行 耗材品类判断
            if($si->type==0){
                $temp_subject=($opei->temp_subject==0)?$old_si->supply_classification_id:$opei->temp_subject;//如果
                if($temp_subject!=$si->supply_classification_id){
                    $response = Lang::get("scs_error.supply_subject_error_41002");
                    break;
                }
            }
            //第一次更换耗材记录该耗材的；品类 为了以后更换标准耗材判断 
            if($opei->temp_subject==0){
                $opei->temp_subject=$old_si->supply_classification_id;
            }

            // $number=Input::get("number");
            // if($number==$opbi->num){
            //     $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id,"order_product_amount"=>$order_product->amount,"order_amount"=>$orders->amount));
            //     break;
            // }
            $response=OrderProductEsItem::changeProductItem($order_product,$opei,$opep,$si,$sc_user->id,$wmcf,$si->type,$csi);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeBaseProductItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:  2017-04-11 15:22:46
     * @Description:修改用户的 
     */
    public function changeEsProductItemBefore(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_esitem_id'=> 'required',//订单耗材的id
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            $opei=OrderProductEsItem::find(Input::get("order_product_esitem_id"));
            if(is_null($opei)||$opei->order_id!=$orders->order_id||$opei->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $opep=OrderProductEsPackage::find($opei->order_product_package_id);
            $order_product=OrderProduct::find($opep->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.order_car_model_notexit_40002");
                break;
            }
            // $whp=WorkHoursPrice::where("city_id",$sc_user->shop->city_id)->first();
            // if(is_null($whp)){
            //     $response = Lang::get("scs_error.work_hour_error_10001");
            //     break;
            // }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }
            $moo=MaintenanceOutOrder::where("product_type",$order_product->product_type)->where("product_item_id",$opei->id)->whereIn("status",array(0,1,2))->first();
            if($moo){//如果存在 备货中或者 等待领料的 出库申请 已经领料的 不允许修改
                $response = Lang::get("scs_error.order_product_item_change_error_40012");
                break;
            }

            
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeBaseProductItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-11-7 11:01:59
     * @Description: 用于无适配耗材更换基础产品包的耗材
     */
    public function changeNoAdapterEsProductItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_esitem_id'=> 'required',//订单耗材的id
                'new_item_id'=> 'required',
                // 'type' => 'required'//0为标准耗材1为非标
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            $opei=OrderProductEsItem::find(Input::get("order_product_esitem_id"));
            if(is_null($opei)||$opei->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $opep=OrderProductEsPackage::find($opei->order_product_package_id);
            $order_product=OrderProduct::find($opep->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.order_car_model_notexit_40002");
                break;
            }
            // $whp=WorkHoursPrice::where("city_id",$sc_user->shop->city_id)->first();
            // if(is_null($whp)){
            //     $response = Lang::get("scs_error.work_hour_error_10001");
            //     break;
            // }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }
            $si=SupplyItem::find(Input::get("new_item_id"));
    
            //要更换成的配件类型判断
            // if(Input::get('type')==0){
                $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->first();
                if(!$mw || $opei->num>$mw->number){
                    $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                    break;
                }
                
                //更换多城市价格
                $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$orders->city)->where("status",1)->first();
                if($sip){
                    $si->amount=$sip->price;
                }else{
                    $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                    $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                }
    
                if(Orders::isSale($si->id,$si->type)){
                    $response = Lang::get("scs_error.supply_error_10011");
                    break;
                }
                //                 $spi=SupplyProductItem::where("car_model_id",$csi->id)->where("supply_item_id",$si->id)->where("status",1)->first();
                //                 if(is_null($spi)){
                //                     $response = Lang::get("scs_error.supply_item_car_model_error_41001");
                //                     break;
                //                 }
    
            // }else{
            //     $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',1)->first();
            //     if(!$mw || $opei->num>$mw->number){
            //         $response = Lang::get("scs_error.supply_item_low_stocks_40005");
            //         break;
            //     }
            //     $si=SupplyItemNonStandard::find(Input::get("new_item_id"));
            //     if(Orders::isSale($si->id,1)){
            //         $response = Lang::get("scs_error.supply_error_10011");
            //         break;
            //     }
            //     $si->amount=Input::get("amount")*100;//如果是非标的 更新非标为传过来的价格
            // }
    
//             if(is_null($si)||$si->status!=1){
//                 $response = Lang::get("scs_error.supply_item_not_exit_41000");
//                 break;
//             }
            //如果更换非标不进行 耗材品类判断
            if($si->type==0){
                if($opei->item_id!=$si->supply_classification_id){//在添加无适配耗材时耗材id存入的是品类id
                    $response = Lang::get("scs_error.supply_subject_error_41002");
                    break;
                }
            }

             $opei->temp_subject=$opei->item_id;
    
            // $number=Input::get("number");
            // if($number==$opbi->num){
            //     $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id,"order_product_amount"=>$order_product->amount,"order_amount"=>$orders->amount));
            //     break;
            // }
            $response=OrderProductEsItem::changeProductItem($order_product,$opei,$opep,$si,$sc_user->id,$wmcf,$si->type,$csi);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeBaseProductItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-2-24 14:01:52
     * @Description: 用于添加 工时
     */
    public function addWorkHours(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                'order_product_id'=> 'required',
                'amount' => 'required|numeric|min:0'
            )
            );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type){
                if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            if($orders->order_type==1){
                $csi=DataCarStandardInfo::find($orders->car_model_id);
                if(is_null($csi)){//只进行 车型存在 不对车型状态进行祥判断
                    $response = Lang::get("scs_error.order_car_model_notexit_40002");
                    break;
                }
            }
            $op=OrderProduct::find(Input::get('order_product_id'));
            if(is_null($op)||$op->product_type!='non_standard'){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            $opns=OrderProductNonStandard::where("order_product_id",$op->id)->where("service_type",2)->where('status',1)->first();
            if($opns){
                $response = Lang::get("scs_error.order_product_work_exit_40032");
                break;
            }
            $opns=new OrderProductNonStandard;
            $opns->order_id=$orders->order_id;
            $opns->name='工时费';
            $opns->num=0;
            $opns->amount=Input::get('amount')*100;
            $opns->order_product_id=Input::get('order_product_id');
            $opns->work_station=0;
            $opns->work_status="prepare";
            $opns->type=1;
            $opns->unit='个';
            $opns->service_type=2;
            $opns->save();

            //每次添加标准或非标配件都需修改order_product及orders中金额
            $op->amount+=Input::get('amount')*100;
            $op->save();

            $orders->amount+=Input::get('amount')*100;
            $orders->balance_amount+=Input::get('amount')*100;
            $orders->save();
            $opr=new OrderProductRecord();
            $opr->order_id=$orders->order_id;
            $opr->product_type='non_standard';
            $opr->operation_type='create';
            $opr->num=0;
            $opr->operation_grade='二级';
            $opr->user_id=$sc_user->id;
            $opr->user_type='sc';
            $opr->operation_id=$opns->id;
            $opr->name=$opns->name;
            $opr->amount=$opns->amount;
            $opr->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addService", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-2-24 14:01:52
     * @Description: 用于修改工时费
     */
    public function updateWorkHours(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                'item_id'=> 'required',//order_product_non_standard的id
                'amount' => 'required|numeric'
            )
            );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                if(!in_array($orders->status,Orders::$add_product)){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }else{
                if($orders->status!=1411){//订单状态是正确
                    $response = Lang::get("scs_error.order_status_error_40022");
                    break;
                }
            }
            $op=OrderPay::where("order_id",$orders->order_id)->whereIn("status",array(0,1,5,7))->first();      
            if($op){//订单状态是否为服务结束状态
                $response = Lang::get("scs_error.order_pay_record_40112");
                break;
            }
            if($orders->order_type==1){
                $csi=DataCarStandardInfo::find($orders->car_model_id);
                if(is_null($csi)){//只进行 车型存在 不对车型状态进行祥判断
                    $response = Lang::get("scs_error.order_car_model_notexit_40002");
                    break;
                }
            }
            $opns=OrderProductNonStandard::where('id',Input::get('item_id'))->where('status',1)->first();
            $op=OrderProduct::find($opns->order_product_id);

            if($opns->order_id!=Input::get('order_id')){
                $response = Lang::get("scs_error.supply_subject_error_41003");
                break;
            }

            $opr=new OrderProductRecord();
            $opr->order_id=$orders->order_id;
            $opr->product_type='non_standard';
            $opr->operation_type='update';
            $opr->num=0;
            $opr->operation_grade='二级';
            $opr->user_id=$sc_user->id;
            $opr->user_type='sc';
            $opr->operation_id=$op->id;
            $opr->name=$opns->name;
            $opr->amount=$opns->amount;
            $opr->save();

            $old_amount=$opns->amount;
            $discount=0;
            if($opns->discount!=1){
                $discount=(Input::get('amount')*100-$opns->amount)*(1-$opns->discount);//多优惠的金额
            }
            $opns->amount=Input::get('amount')*100;
            $opns->save();
            
            //每次添加标准或非标配件都需修改order_product及orders中金额
            $op->amount=$op->amount+Input::get('amount')*100-$old_amount;
            $op->discount=$op->discount+$discount;
            $op->save();

            $orders->amount=$orders->amount+Input::get('amount')*100-$old_amount;
            $orders->balance_amount=$orders->balance_amount+Input::get('amount')*100-$old_amount-$discount;
            $orders->discount=$orders->discount+$discount;
            $orders->save();

            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addService", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:zhanghu
     * @Description:保存门店对企业用户的行动方案
     */
    public function storeShopsEsPlan(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'shop_id' => 'required',
                'user_id'=> 'required',//
            )
        );
        $response = array('code' => 0, 'msg' => '保存成功');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                $response = array('code' => -1, 'msg' => '登录验证失败');
                break;
            }
            $id = Input::get('id');
            if($id) {
                $shopty = ShopUserProperty::find($id);
                if(!$shopty){
                    $response = array('code' => -1, 'msg' => '找不到这个方案');
                    break;
                }
            }else{
                $shopty = new ShopUserProperty();
            }
            $shopty->shop_id = Input::get('shop_id');
            $shopty->user_id = Input::get('user_id');
            $shopty->user_type = 2;
            $shopty->user_comments = Input::get('user_comments');
            $shopty->created_by = $sc_user->id;
            try {
                $shopty->save();
            }catch(Exception $e){
                $response = array('code' => -1, 'msg' => '保存失败');
                break;
            }
            $response["result"] = $shopty;
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "storeShopsEsPlan", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:zhanghu
     * @Description:获取门店对企业用户的行动方案
     */
    public function getShopsEsPlan(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'shop_id' => 'required',
                'user_id'=> 'required',//
            )
        );
        $response = array('code' => 0, 'msg' => 'ok',"result"=>array());
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["code"] = -1;
                $response["result"] = $validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                $response = array('code' => -1, 'msg' => '登录验证失败');
                break;
            }
            $shopty = ShopUserProperty::where('shop_id',Input::get('shop_id'))
                ->where('user_id',Input::get('user_id'))
                ->where('user_type',2)
                ->orderBy('updated_at','desc')
                ->first();
            if(!$shopty){
                $response = array('code' => 0, 'msg' => 'ok',"result" => new stdClass());
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok',"result"=>$shopty);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "storeShopsEsPlan", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}